package com.juyu.miliao.api.controller;

import com.juyu.miliao.api.domain.SignIn;
import com.juyu.miliao.api.domain.vo.SignInVo;
import com.juyu.miliao.api.service.SignInService;
import com.juyu.miliao.common.domain.Subject;
import com.juyu.miliao.common.exception.IllegalParamException;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;

/**
 * @ClassName: SignInController
 * @Description: 签到有礼
 * @Author zhangcheng
 * @Date 19-9-11下午1:31
 */
@RestController
public class SignInController {
    @Resource
    private SignInService signInService;

    @GetMapping("/signIn")
    public SignIn getSignInData(Authentication token) {
        Subject subject = (Subject) token.getPrincipal();
        long userId = subject.getId();
        return signInService.getSignInData(userId);
    }

    @PostMapping("/signIn")
    public SignInVo signIn(Authentication token) {
        Subject subject = (Subject) token.getPrincipal();
        long userId = subject.getId();
        return signInService.signIn(userId);
    }

    @PostMapping("/vipSignInReward")
    public void award(Authentication token, @RequestParam("vipRewardId") Long vipRewardId) {
        if(vipRewardId == null || vipRewardId < 0 || vipRewardId > 5) {
            throw new IllegalParamException("");
        }
        Subject subject = (Subject) token.getPrincipal();
        long userId = subject.getId();
        signInService.award(userId, vipRewardId);
    }

}
